package com.example.anydo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.anydo.R;
import com.example.anydo.constant.Music;
import com.example.anydo.util.pub_util.MusicPlayer;
import com.example.anydo.util.pub_util.SPUtil;
import com.example.anydo.util.pub_util.Util;

import java.util.Calendar;
import java.util.Locale;
import java.util.zip.Inflater;

public class TomatoAlarmActivity extends AppCompatActivity {

    int hourNow = 24;
    int minuteNow = 59;
    int secondNow = 59;
    Thread thread;
    ImageView stopFoucsimg;
    TextView stopFoucstext;
    ImageView startFoucsimg;
    TextView startFoucstext;
    TextView timeText;
    TextView foucsOn;
    TextView foucsOnText;
    boolean threadExit = false;
    private TextView tv_music;
    private int pos;
    private MusicPlayer player;
    private TextView tv_date;
    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tomato_alarm);

        tv_date = findViewById(R.id.tv_date);
        tv_title = findViewById(R.id.tv_title);
        tv_music = findViewById(R.id.tv_music);
        foucsOnText = findViewById(R.id.foucsOnText);
        stopFoucsimg = findViewById(R.id.stop_foucs_img);
        stopFoucstext = findViewById(R.id.stop_foucs);
        startFoucsimg = findViewById(R.id.start_foucs_img);
        startFoucstext = findViewById(R.id.start_foucs);
        timeText = findViewById(R.id.time_selector);
        foucsOn = findViewById(R.id.foucs_on);
        stopFoucsimg.setVisibility(View.INVISIBLE);
        stopFoucstext.setVisibility(View.INVISIBLE);

        Intent intent = getIntent();
        tv_title.setText( intent.getStringExtra("title"));
        tv_date.setText(Util.getDayNowShow());

        player = new MusicPlayer(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        pos = (int)SPUtil.getData(this,SPUtil.KEY_MUSIC,0);
        if(pos == 0){
            tv_music.setText("静音");
        }else {
            tv_music.setText(Music.music[pos-1]);
        }
    }

    public  void changeTime()
    {
        if(secondNow == 0)
        {
            secondNow = 60;
            if(minuteNow == 0)
            {
                minuteNow = 60;
                hourNow -= 1;
            }

            minuteNow -= 1;
        }
        secondNow -= 1;

    }

    public void setTimeText()
    {
        String hourString;
        String minuteString;
        String secondString;

        hourString = Integer.toString(hourNow);
        minuteString = Integer.toString(minuteNow);
        secondString = Integer.toString(secondNow);

        if(hourNow < 10)
            hourString = "0" + hourString;
        if(minuteNow < 10)
            minuteString = "0" + minuteString;
        if(secondNow < 10)
            secondString = "0" + secondString;
        timeText.setText(hourString + ":" + minuteString + ":" + secondString);
        foucsOnText.setText(hourString + ":" + minuteString + ":" + secondString);
    }

    public void return_click(View view)
    {
        finish();
    }

    public void select_time(View view)
    {
        Calendar calendar= Calendar.getInstance(Locale.CHINA);
        new TimePickerDialog( this,
                // 绑定监听器
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        hourNow = hourOfDay;
                        minuteNow = minute;
                        secondNow = 0;
                        setTimeText();
                    }
                }
                // 设置初始时间
                , calendar.get(Calendar.HOUR_OF_DAY)
                , calendar.get(Calendar.MINUTE)
                // true表示采用24小时制
                ,true).show();
    }
    public void select_music(View view)
    {
        player.stop();
        Intent intent = new Intent(this,MusicActivity.class);
        startActivity(intent);
    }
    public  void  startFoucs(View view)
    {
        stopFoucsimg.setVisibility(View.VISIBLE);
        stopFoucstext.setVisibility(View.VISIBLE);
        foucsOn.setVisibility(View.VISIBLE);
        startFoucsimg.setVisibility(View.INVISIBLE);
        startFoucstext.setVisibility(View.INVISIBLE);


        threadExit = false;
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while ((hourNow != 0 || minuteNow != 0 || secondNow != 0 )&& !threadExit) {
                    changeTime();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setTimeText();
                        }
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
        if(pos > 0){
            player.play(pos - 1);
        }
    }
    public  void  stopFoucs(View view)
    {
        stopFoucsimg.setVisibility(View.INVISIBLE);
        stopFoucstext.setVisibility(View.INVISIBLE);
        startFoucsimg.setVisibility(View.VISIBLE);
        startFoucstext.setVisibility(View.VISIBLE);
        threadExit = true;
        player.stop();
    }

    @Override
    protected void onDestroy() {

        player.onDestroy();
        super.onDestroy();
    }

    public void foucsOn(View view) {
        foucsOnText.setVisibility(View.VISIBLE);
    }

    public void foucsOnTextClick(View view) {
        foucsOnText.setVisibility(View.INVISIBLE);
    }
}